MySQL : MySQL database client
This is a JSREclient driver for MySQL.
User can use the following code to import the mysql module.
var mysql = require('mysql');
Here is an example on how to use it:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : '192.168.128.101',
user : 'me',
password : 'secret',
database : 'my_db'
});
connection.connect();
connection.query('SELECT 1 + 1 AS solution', function(error, results, fields) {
if (error) throw error;
console.log('The solution is: ', results[0].solution);
});
connection.end();
require('iosched').forever();
From this example, you can learn the following:
- Every method you invoke on a connection is queued and executed in sequence.
- Closing the connection is done using
end()which makes sure all remaining queries are executed before sending a quit packet to theMySQLserver.
Support
The following shows mysql module APIs available for each permissions.
| User Mode | Privilege Mode | |
|---|---|---|
| mysql | ● | ● |
| mysql.createConnection | ● | ● |
| mysql.createPool | ● | ● |
| mysql.createPoolCluster | ● | ● |
| mysql.escape | ● | ● |
| mysql.escapeId | ● | ● |
| mysql.format | ● | ● |
| mysql.raw | ● | ● |
| mysql.Types | ● | ● |
| Connection | ● | ● |
| connection.threadId | ● | ● |
| connection.connect | ● | ● |
| connection.changeUser | ● | ● |
| connection.beginTransaction | ● | ● |
| connection.commit | ● | ● |
| connection.rollback | ● | ● |
| connection.query | ● | ● |
| connection.ping | ● | ● |
| connection.end | ● | ● |
| connection.destroy | ● | ● |
| connection.pause | ● | ● |
| connection.resume | ● | ● |
| connection.escape | ● | ● |
| connection.escapeId | ● | ● |
| connection.format | ● | ● |
| Query | ● | ● |
| query.stream | ● | ● |
| PoolConnection | ● | ● |
| poolConnection.release | ● | ● |
| poolConnection.end | ● | ● |
| poolConnection.destroy | ● | ● |
| Pool | ● | ● |
| pool.getConnection | ● | ● |
| pool.releaseConnection | ● | ● |
| pool.end | ● | ● |
| pool.query | ● | ● |
| pool.escape | ● | ● |
| pool.escapeId | ● | ● |
| PoolCluster | ● | ● |
| poolCluster.add | ● | ● |
| poolCluster.remove | ● | ● |
| poolCluster.of | ● | ● |
| poolCluster.getConnection | ● | ● |
| poolCluster.end | ● | ● |
| PoolNamespace | ● | ● |
| poolNamespace.getConnection | ● | ● |
| poolNamespace.query | ● | ● |
Mysql Object
mysql.createConnection(config)
config{Object | String} Config configuration or connection string for newmysqlconnection.- Returns: {Connection} A new
mysqlconnection.
Create a new Connection instance.
Connection Options
When establishing a connection, you can set the following config options:
host{String} The hostname of the database you are connecting to. default: 'localhost'port{Integerr} The port number to connect to. default: 3306family{Integer} Version of IP stack. Must be4,6. default:4.user{String} TheMySQLuser to authenticate as.password{String} The password of thatMySQLuser.database{String} Name of the database to use for this connection (Optional).charset{String} The charset for the connection. This is called "collation" in the SQL-level ofMySQL(likeutf8_general_ci). If a SQL-level charset is specified (likeutf8mb4) then the default collation for that charset is used. default: 'UTF8_GENERAL_CI'timezone{String} The timezone configured on theMySQLserver. This is used to type cast server date/time values to JavaScriptDateobject and vice versa. This can be'local','Z', or an offset in the form+HH:MMor-HH:MM. default:'local'connectTimeout{Integer} The milliseconds before a timeout occurs during the initial connection to theMySQLserver. default: 10000stringifyObjects{Boolean} Stringify objects instead of converting to values. default: falseinsecureAuth{Boolean} Allow connecting tomysqlinstances that ask for the old (insecure) authentication method. default: falsetypeCast{Boolean | Function} Determines if column values should be converted to native JavaScript types. The usage of functiontypeCastsee Custom Type Casting. default: truequeryFormat{Function} A custom query format function. See Preparing Queries.supportBigNumbers{Boolean} When dealing with big numbers (BIGINTandDECIMALcolumns) in the database, you should enable this option default: falsebigNumberStrings{Boolean} Enabling bothsupportBigNumbersandbigNumberStringsforces big numbers (BIGINTandDECIMALcolumns) to be always returned as JavaScript String objects default: false EnablingsupportBigNumbersbut leavingbigNumberStringsdisabled will return big numbers as string objects only when they cannot be accurately represented withNumberobjects (which happens when they exceed the [-2^53, +2^53] range), otherwise they will be returned as number objects. This option is ignored ifsupportBigNumbersis disabled.dateStrings{Boolean} Force date types (TIMESTAMP,DATETIME,DATE) to be returned as strings rather than inflated into JavaScript Date objects. Can betrue/falseor an array of type names to keep as strings. default: falsedebug{Boolean | String} Prints protocol details to stdout. Can betrue/falseor an array of packet type names that should be printed. default: falsetrace{Boolean} Generates stack traces onErrorto include call site of library entrance ("long stack traces"). Slight performance penalty for most calls. default: truelocalInfile{Boolean} AllowLOAD DATA INFILEto use theLOCALmodifier. default: truemultipleStatements{Boolean} Allow multiple mysql statements per query. Be careful with this, it could increase the scope ofSQLinjection attacks. default: falseflags{String} List of connection flags to use other than the default ones. It is also possible to blacklist default ones. For more information, check Connection Flags.tlsOpt{Object} TLS securely connections options. default: undefined, means use TCP connection.
In addition to passing these options as an object, you can also use a url string. For example:
var connection = mysql.createConnection('mysql://user:pass@host/db?debug=true&charset=BIG5_CHINESE_CI&timezone=-0700');
Connection Flags
If, for any reason, you would like to change the default connection flags, you can use the connection option flags. Pass a string with a comma separated list of items to add to the default flags. If you don't want a default flag to be used prepend the flag with a minus sign. To add a flag that is not in the default list, just write the flag name, or prefix it with a plus (case insensitive).
var connection = mysql.createConnection({
// disable FOUND_ROWS flag, enable IGNORE_SPACE flag
flags: '-FOUND_ROWS,IGNORE_SPACE'
});
The following flags are available:
CONNECT_WITH_DB- Ability to specify the database on connection. default: onFOUND_ROWS- Send the found rows instead of the affected rows asaffectedRows. default: onIGNORE_SPACE- Let the parser ignore spaces before the(in queries.default: onINTERACTIVE- Indicates to theMySQLserver this is an "interactive" client. This will use the interactive timeouts on theMySQLserver and report as interactive in the process list. default: offLOCAL_FILES- Can useLOAD DATA LOCAL. This flag is controlled by the connection optionlocalInfile.default: onLONG_FLAG- Longer flags inProtocol::ColumnDefinition320. default: onLONG_PASSWORD- Use the improved version ofOld Password Authentication.default: onMULTI_RESULTS- Can handle multiple resultsets for queries. default: onMULTI_STATEMENTS- The client may send multiple statement per query or statement prepare (separated by;). This flag is controlled by the connection optionmultipleStatements.default: offPROTOCOL_41- Uses the 4.1 protocol. default: onPS_MULTI_RESULTS- Can handle multiple resultsets for execute. default: onRESERVED- Old flag for the 4.1 protocol. default: onSECURE_CONNECTION- Support native 4.1 authentication. default: onTRANSACTIONS- Asks for the transaction status flags.default: on
Example
The recommended way to establish a connection is this:
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'example.org',
user: 'bob',
password: 'secret'
});
connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
console.log('connected as id ' + connection.threadId);
});
However, a connection can also be implicitly established by invoking a query:
var mysql = require('mysql');
var connection = mysql.createConnection(config);
connection.query('SELECT 1', function(error, results, fields) {
if (error) throw error;
// connected!
});
Depending on how you like to handle your errors, either method may be appropriate. Any type of connection error (handshake or network) is considered a fatal error, see the Error Handling section for more information.
mysql.createPool(config)
config{Object | String} Config configuration or connection string for newMySQLconnections.- Returns: {Pool} A new
MySQLpool.
Create a new Pool instance.
Rather than creating and managing connections one-by-one, this module also provides built-in connection pooling using mysql.createPool(config).
Pools accept all the same options as a connection. When creating a new connection, the options are simply passed to the connection constructor. In addition to those options pools accept a few extras:
acquireTimeout{Integer} The milliseconds before a timeout occurs during the connection acquisition. This is slightly different fromconnectTimeout, because acquiring a pool connection does not always involve making a connection. If a connection request is queued, the time the request spends in the queue does not count towards this timeout. default: 10000waitForConnections{Boolean} Determines the pool's action when no connections are available and the limit has been reached. Iftrue, the pool will queue the connection request and call it when one becomes available. Iffalse, the pool will immediately call back with an error. default: trueconnectionLimit{Integer} The maximum number of connections to create at once. default: 10queueLimit{Integer} The maximum number of connection requests the pool will queue before returning an error fromgetConnection. If set to0, there is no limit to the number of queued connection requests. default: 0
Example
Create a pool and use it directly:
var mysql = require('mysql');
var pool = mysql.createPool({
connectionLimit: 10,
host: 'example.org',
user: 'bob',
password: 'secret',
database: 'my_db'
});
pool.query('SELECT 1 + 1 AS solution', function(error, results, fields) {
if (error) throw error;
console.log('The solution is: ', results[0].solution);
});
This is a shortcut for the pool.getConnection() -> connection.query() -> connection.release() code flow. Using pool.getConnection() is useful to share connection state for subsequent queries. This is because two calls to pool.query() may use two different connections and run in parallel. This is the basic structure:
var mysql = require('mysql');
var pool = mysql.createPool(config);
pool.getConnection(function(err, connection) {
if (err) throw err; // not connected!
// Use the connection
connection.query('SELECT something FROM sometable', function (error, results, fields) {
// When done with the connection, release it.
connection.release();
// Handle error after the release.
if (error) throw error;
// Don't use the connection here, it has been returned to the pool.
});
});
If you would like to close the connection and remove it from the pool, use connection.destroy() instead. The pool will create a new connection the next time one is needed.
Connections are lazily created by the pool. If you configure the pool to allow up to 100 connections, but only ever use 5 simultaneously, only 5 connections will be made. Connections are also cycled round-robin style, with connections being taken from the top of the pool and returning to the bottom.
When a previous connection is retrieved from the pool, a ping packet is sent to the server to check if the connection is still good.
mysql.createPoolCluster(config)
config{Object} Configuration for pool clusterconnections.- Returns: {Pool} New
MySQLpool cluster.
Create a new PoolCluster instance. PoolCluster provides multiple hosts connection.
PoolCluster Options
canRetry{Boolean} Iftrue,PoolClusterwill attempt to reconnect when connection fails. default: trueremoveNodeErrorCount{Integer} If connection fails, node'serrorCountincreases. WhenerrorCountis greater thanremoveNodeErrorCount, remove a node in thePoolCluster. default: 5restoreNodeTimeout{Integer} If connection fails, specifies the number of milliseconds before another connection attempt will be made. If set to0, then node will be removed instead and never re-used. default: 0defaultSelector{String} The default selector. default:RRRR: Select one alternately. (Round-Robin)RANDOM: Select the node by random function.ORDER: Select the first node available unconditionally.
var clusterConfig = {
removeNodeErrorCount: 1, // Remove the node immediately when connection fails.
defaultSelector: 'ORDER'
};
var poolCluster = mysql.createPoolCluster(clusterConfig);
Example
// create
var poolCluster = mysql.createPoolCluster();
// add configurations (the config is a pool config object)
poolCluster.add(config); // add configuration with automatic name
poolCluster.add('MASTER', masterConfig); // add a named configuration
poolCluster.add('SLAVE1', slave1Config);
poolCluster.add('SLAVE2', slave2Config);
// remove configurations
poolCluster.remove('SLAVE2'); // By nodeId
poolCluster.remove('SLAVE*'); // By target group : SLAVE1-2
// Target Group : ALL(anonymous, MASTER, SLAVE1-2), Selector : round-robin(default)
poolCluster.getConnection(function(err, connection) { });
// Target Group : MASTER, Selector : round-robin
poolCluster.getConnection('MASTER', function(err, connection) { });
// Target Group : SLAVE1-2, Selector : order
// If can't connect to SLAVE1, return SLAVE2. (remove SLAVE1 in the cluster)
poolCluster.on('remove', function(nodeId) {
console.log('REMOVED NODE : ' + nodeId); // nodeId = SLAVE1
});
// A pattern can be passed with * as wildcard
poolCluster.getConnection('SLAVE*', 'ORDER', function(err, connection) { });
// The pattern can also be a regular expression
poolCluster.getConnection(/^SLAVE[12]$/, function(err, connection) { });
// of namespace : of(pattern, selector)
poolCluster.of('*').getConnection(function(err, connection) { });
var pool = poolCluster.of('SLAVE*', 'RANDOM');
pool.getConnection(function(err, connection) { });
pool.getConnection(function(err, connection) { });
pool.query(function(error, results, fields) { });
// close all connections
poolCluster.end(function(err) {
// all connections in the pool cluster have ended
});
mysql.escape(value[, stringifyObjects[, timeZone]])
value{Any} The value to escape.stringifyObjects{Boolean} Setting if objects should be stringified. default: falsetimeZone{String} Setting for time zone to use forDateconversion. default: local- Returns: {String} Escaped string value.
Escape a value for SQL. Detali to see Escaping Query Values.
mysql.escapeId(value[, forbidQualified])
value{Any} The value to escape.forbidQualified{Boolean} Setting to treat '.' as part of identifier. default: false- Returns: {String} Escaped string value.
Escape an identifier for SQL. Detali to see Escaping Query Identifiers.
mysql.format(sql, values[, stringifyObjects[, timeZone]])
sql{String} TheSQLfor the query.values{Array} Any values to insert into placeholders in sql.stringifyObjects{Boolean} Setting if objects should be stringified. default: falsetimeZone{String} Setting for time zone to use forDateconversion. default: local- Returns: {String} Formatted
SQLstring.
Format SQL and replacement values into a SQL string. Detali to see Preparing Queries.
mysql.raw(sql)
sql{String} The rawSQL- Returns: {Object} Wrapped object.
Wrap raw SQL strings from escape overriding. Detali to see Escaping Query Values.
mysql.Types
- {Object}
For your convenience, this driver will cast mysql types into native JavaScript types by default. The following mappings exist:
- Number
TINYINTSMALLINTINTMEDIUMINTYEARFLOATDOUBLE
- Date
TIMESTAMPDATEDATETIME
- Buffer
TINYBLOBMEDIUMBLOBLONGBLOBBLOBBINARYVARBINARYBIT(last byte will be filled with 0 bits as necessary)
- String
CHARVARCHARTINYTEXTMEDIUMTEXTLONGTEXTTEXTENUMSETDECIMAL(may exceed float precision)BIGINT(may exceed float precision)TIME(could be mapped to Date, but what date would be set?)GEOMETRY(never used those, get in touch if you do)
It is not recommended (and may go away / change in the future) to disable type casting, but you can currently do so on either the connection:
var connection = require('mysql').createConnection({typeCast: false});
Or on the query level:
var options = {sql: '...', typeCast: false};
var query = connection.query(options, function(error, results, fields) {
if (error) throw error;
// ...
});
Example
// Get type
console.log(mysql.Types.VAR_STRING); // 253
// Get type name
console.log(mysql.Types[253]); // 'VAR_STRING'
Custom Type Casting
You can also pass a function and handle type casting yourself. You're given some column information like database, table and name and also type and length. If you just want to apply a custom type casting to a specific type you can do it and then fallback to the default.
The function is provided two arguments field and next and is expected to return the value for the given field by invoking the parser functions through the field object.
The field argument is a Field object and contains data about the field that need to be parsed. The following are some of the properties on a Field object:
db- a string of the database the field came from.table- a string of the table the field came from.name- a string of the field name.type- a string of the field type in all caps.length- a number of the field length, as given by the database.
The next argument is a function that, when called, will return the default type conversion for the given field.
When getting the field data, the following helper methods are present on the field object:
.string()- parse the field into a string..buffer()- parse the field into aBuffer..geometry()- parse the field as a geometry value.
The MySQL protocol is a text-based protocol. This means that over the wire, all field types are represented as a string, which is why only string-like functions are available on the field object. Based on the type information (like INT), the type cast should convert the string field into a different JavaScript type (like a number).
Here's an example of converting TINYINT(1) to boolean:
connection = mysql.createConnection({
typeCast: function(field, next) {
if (field.type === 'TINY' && field.length === 1) {
return (field.string() === '1'); // 1 = true, 0 = false
} else {
return next();
}
}
});
Connection Object
connection.threadId
- {String} You can get the
MySQLconnection ID ("thread ID") of a given connection using thethreadIdproperty.
Example
connection.connect(function(err) {
if (err) throw err;
console.log('connected as id ' + connection.threadId);
});
connection.connect([options][, callback])
options{Object} Connect options.timeout{Integer} Connect timeout (ms).
callback[Function] Connect callback, arguments:err{Error | undefined}
Connect to server.
connection.changeUser([options][, callback])
options{Object} Change user options.callback[Function] Arguments:err{Error | undefined}
MySQL offers a changeUser command that allows you to alter the current user and other aspects of the connection without shutting down the underlying socket:
connection.changeUser({user : 'john'}, function(err) {
if (err) throw err;
});
The available options for this feature are:
user{String} The name of the new user. default: the previous onepassword{String} The password of the new user. default: the previous onecharset{String} The new charset. default: the previous onedatabase{String} The new database. default: the previous one
A sometimes useful side effect of this functionality is that this function also resets any connection state (variables, transactions, etc.).
Errors encountered during this operation are treated as fatal connection errors by this module.
connection.beginTransaction([options][, callback])
options{Object} Command options.timeout{Integer} Command timeout (ms).
callback[Function] Command callback, arguments:err{Error | undefined}
Begin transaction.
Detali to see Transactions.
connection.commit([options][, callback])
options{Object} Command options.timeout{Integer} Command timeout (ms).
callback[Function] Command callback, arguments:err{Error | undefined}
Commit transaction.
Detali to see Transactions.
connection.rollback([options][, callback])
options{Object} Command options.timeout{Integer} Command timeout (ms).
callback[Function] Command callback, arguments:err{Error | undefined}
Rollback transaction.
Detali to see Transactions.
connection.query(sql[, values][, cb])
sql{String | Object} Sql string or query options.values{Any} Values to be format.cb{Function} Callback, arguments:error{Error | undefined} Error if one occurred during the query.results{Array | Object} The results of the query.fields{Array | undefined} Information about the returned results fields (if any).
- Returns: {Query} Query object.
Query usage
The most basic way to perform a query is to call the query() method on an object (like a Connection, Pool, or PoolNamespace instance).
The simplest form of ..query() is .query(sql, callback), where a SQL string is the first argument and the second is a callback:
connection.query('SELECT * FROM `books` WHERE `author` = "David"', function(error, results, fields) {
// error will be an Error if one occurred during the query
// results will contain the results of the query
// fields will contain information about the returned results fields (if any)
});
The second form .query(sql, values, callback) comes when using placeholder values (see: Escaping Query Values):
connection.query('SELECT * FROM `books` WHERE `author` = ?', ['David'], function(error, results, fields) {
// error will be an Error if one occurred during the query
// results will contain the results of the query
// fields will contain information about the returned results fields (if any)
});
The third form .query(options, callback) comes when using various advanced options on the query, like:
connection.query({
sql: 'SELECT * FROM `books` WHERE `author` = ?',
timeout: 40000, // 40s
values: ['David']
}, function(error, results, fields) {
// error will be an Error if one occurred during the query
// results will contain the results of the query
// fields will contain information about the returned results fields (if any)
});
Note that a combination of the second and third forms can be used where the placeholder values are passed as an argument and not in the options object. The values argument will override the values in the option object.
connection.query({
sql: 'SELECT * FROM `books` WHERE `author` = ?',
timeout: 40000, // 40s
}, ['David'], function(error, results, fields) {
// error will be an Error if one occurred during the query
// results will contain the results of the query
// fields will contain information about the returned results fields (if any)
});
If the query only has a single replacement character (?), and the value is not null, undefined, or an array, it can be passed directly as the second argument to .query:
connection.query(
'SELECT * FROM `books` WHERE `author` = ?',
'David',
function(error, results, fields) {
// error will be an Error if one occurred during the query
// results will contain the results of the query
// fields will contain information about the returned results fields (if any)
}
);
The query fields
The general query fields is an array containing each field of data objects.
connection.query('SELECT * FROM student', function(error, results, fields) {
if (error) throw error;
console.log(fields[0].name); // id
});
Field options:
db*{String} * Database name.table*{String} * Table name.name*{String} * Field name.type*{Integer} * Field type, see: mysql.Types.length*{Integer} * Field type length (bytes).
The query rows
The general query results is an array containing each row of data objects.
connection.query('SELECT * FROM student', function(error, results, fields) {
if (error) throw error;
console.log(results); // [{id: 1, name: 'name1'}, ...]
});
Getting the id of an inserted row
If you are inserting a row into a table with an auto increment primary key, you can retrieve the insert id like this:
connection.query('INSERT INTO posts SET ?', {title: 'test'}, function(error, results, fields) {
if (error) throw error;
console.log(results.insertId);
});
When dealing with big numbers (above JavaScript Number precision limit), you should consider enabling supportBigNumbers option to be able to read the insert id as a string, otherwise it will throw an error.
This option is also required when fetching big numbers from the database, otherwise you will get values rounded to hundreds or thousands due to the precision limit.
Getting the number of affected rows
You can get the number of affected rows from an insert, update or delete statement.
connection.query('DELETE FROM posts WHERE title = "wrong"', function(error, results, fields) {
if (error) throw error;
console.log('deleted ' + results.affectedRows + ' rows');
})
Getting the number of changed rows
You can get the number of changed rows from an update statement.
changedRows differs from affectedRows in that it does not count updated rows whose values were not changed.
connection.query('UPDATE posts SET ...', function(error, results, fields) {
if (error) throw error;
console.log('changed ' + results.changedRows + ' rows');
})
Multiple statement queries
Support for multiple statements is disabled for security reasons (it allows for SQL injection attacks if values are not properly escaped). To use this feature you have to enable it for your connection:
var connection = mysql.createConnection({multipleStatements: true});
Once enabled, you can execute multiple statement queries like any other query:
connection.query('SELECT 1; SELECT 2', function(error, results, fields) {
if (error) throw error;
// `results` is an array with one element for every statement in the query:
console.log(results[0]); // [{1: 1}]
console.log(results[1]); // [{2: 2}]
});
Additionally you can also stream the results of multiple statement queries:
var query = connection.query('SELECT 1; SELECT 2');
query
.on('fields', function(fields, index) {
// the fields for the result rows that follow
})
.on('result', function(row, index) {
// index refers to the statement this result belongs to (starts at 0)
});
If one of the statements in your query causes an error, the resulting Error object contains a err.index property which tells you which statement caused it. MySQL will also stop executing any remaining statements when an error occurs.
Joins with overlapping column names
When executing joins, you are likely to get result sets with overlapping column names.
By default, mysql will overwrite colliding column names in the order the columns are received from MySQL, causing some of the received values to be unavailable.
However, you can also specify that you want your columns to be nested below the table name like this:
var options = {sql: '...', nestTables: true};
connection.query(options, function(error, results, fields) {
if (error) throw error;
/* results will be an array like this now:
[{
table1: {
fieldA: '...',
fieldB: '...',
},
table2: {
fieldA: '...',
fieldB: '...',
},
}, ...]
*/
});
Or use a string separator to have your results merged.
var options = {sql: '...', nestTables: '_'};
connection.query(options, function(error, results, fields) {
if (error) throw error;
/* results will be an array like this now:
[{
table1_fieldA: '...',
table1_fieldB: '...',
table2_fieldA: '...',
table2_fieldB: '...',
}, ...]
*/
});
connection.ping([options][, callback])
options{Object} Ping options.timeout{Integer} Ping timeout (ms).
callback[Function] Ping callback, arguments:err{Error | undefined}
A ping packet can be sent over a connection using the connection.ping method. This method will send a ping packet to the server and when the server responds, the callback will fire. If an error occurred, the callback will fire with an error argument.
Example
connection.ping(function(err) {
if (err) throw err;
console.log('Server responded to ping');
})
connection.end([options][, callback])
options{Object} Ping options.timeout{Integer} Ping timeout (ms).
callback[Function] Ping callback, arguments:err{Error | undefined}
There are two ways to end a connection. Terminating a connection gracefully is done by calling the end() method:
connection.end(function(err) {
// The connection is terminated now
});
This will make sure all previously enqueued queries are still before sending a COM_QUIT packet to the MySQL server. If a fatal error occurs before the COM_QUIT packet can be sent, an err argument will be provided to the callback, but the connection will be terminated regardless of that.
An alternative way to end the connection is to call the destroy() method. This will cause an immediate termination of the underlying socket. Additionally destroy() guarantees that no more events or callbacks will be triggered for the connection.
connection.destroy();
Unlike end() the destroy() method does not take a callback argument.
connection.destroy()
Destroy a connection.
This will cause an immediate termination of the underlying socket, detail to see: connection.end.
connection.pause()
Pause the underlying socket and connection protocol. Detail to see: Streaming Query Rows
connection.resume()
Resume the underlying socket and connection protocol. Detail to see: Streaming Query Rows
connection.escape(value)
value{Any} The value to escape.- Returns: {String} Escaped string value.
Escape a value for SQL. Detali to see, Escaping Query Values.
Default escape options to see mysql.escape, the timeZone is set by connection.config.timezone.
connection.escapeId(value)
value{Any} The value to escape.- Returns: {String} Escaped string value.
Escape an identifier for SQL. Detali to see Escaping Query Identifiers.
Default escape options to see mysql.escapeId.
connection.format(sql, values)
sql{String} TheSQLfor the query.values{Array} Any values to insert into placeholders in sql.- Returns: {String} Formatted
SQLstring.
Format SQL and replacement values into a SQL string. Detali to see Preparing Queries.
Default escape options to see mysql.format. The stringifyObjects, timeZone options set by connection.config.timezone, connection.config.timezone respectively.
Query Object
Sometimes you may want to select large quantities of rows and process each of them as they are received. This can be done like this:
var query = connection.query('SELECT * FROM st');
query.on('error', function(err) {
// Handle error, an 'end' event will be emitted after this as well
})
.on('fields', function(fields) {
// the field packets for the rows to follow
})
.on('result', function(row) {
// Pausing the connnection is useful if your processing involves I/O
connection.pause();
processRow(row, function() {
connection.resume();
});
})
.on('end', function() {
// all rows have been received
});
Please note a few things about the example above:
- Usually you will want to receive a certain amount of rows before starting to throttle the connection using
pause(). This number will depend on the amount and size of your rows. pause()/resume()operate on the underlying socket and parser. You are guaranteed that no more'result'events will fire after callingpause().- You must not provide a callback to the
query()method when streaming rows. - The
'result'event will fire for both rows as well as OK packets confirming the success of aINSERT/UPDATEquery. - It is very important not to leave the result paused too long, or you may encounter
Error: Connection lost: The server closed the connection.The time limit for this is determined by the net_write_timeout setting on yourMySQLserver.
query.stream([options])
options{Object} TheReadablestream options. Details to see: new stream.Readable([options]).- Returns: {Readable} A
Readablestream.
The query object provides a convenience method .stream([options]) that wraps query events into a Readable stream object. This stream can easily be piped downstream and provides automatic pause/resume, based on downstream congestion and the optional highWaterMark.
The objectMode parameter of the stream is set to true and cannot be changed (if you need a byte stream, you will need to use a transform stream).
For example, piping query results into another stream (with a max buffer of 5 objects) is simply:
connection.query('SELECT * FROM posts')
.stream({highWaterMark: 5})
.pipe(...);
Query Event
error
err{Error} Error object. Handle error, an 'end' event will be emitted after this as well.
fields
fields{Array} Information about the returned results fields, detail to see: connection.query.
result
row{Object} The query row object or other result, detail to see: connection.query.
end
Query end, all rows have been received.
PoolConnection Object
PoolConnection inherits from the Connection class. The poolConnection object is created by the pool object.
poolConnection.release()
Free poolConnection, this object can be obtained from pool next time.
poolConnection.release() is equivalent to pool.releaseConnection(connection).
Example
pool.getConnection(function(err, connection) {
connection.release();
});
poolConnection.end()
Calling poolConnection.end() to release a pooled connection is deprecated. It calls poolConnection.release().
poolConnection.destroy()
The poolConnection destroy and remove from pool. Detail to see: connection.destroy.
Pool Object
The pool object create by mysql.createPool.
The MySQL protocol is sequential, this means that you need multiple connections to execute queries in parallel. You can use a Pool to manage connections, one simple approach is to create one connection per incoming http request.
pool.getConnection(callback)
callback{Function} Arguments:error{Error | undefined} Error object.connection[PoolConnection] Connection object.
Get connection object.
Example
pool.getConnection(function(err, connection) {});
pool.releaseConnection(connection)
connection[PoolConnection] Connection object.
Free connection get by pool.getConnection. connection can be obtained next time.
Example
pool.getConnection(function(err, connection) {
pool.releaseConnection(connection);
});
pool.end(callback)
callback{Function} Arguments:error{Error | undefined} Error object.
When you are done using the pool, you have to end all the connections. This is typically done if the pool is used in a script or when trying to gracefully shutdown a server. To end all the connections in the pool, use the end method on the pool:
pool.end(function(err) {
// all connections in the pool have ended
});
The end method takes an optional callback that you can use to know when all the connections are ended.
pool.end calls connection.end on every active connection in the pool. This queues a QUIT packet on the connection and sets a flag to prevent pool.getConnection from creating new connections. All commands / queries already in progress will complete, but new commands won't execute.
pool.query(sql[, values][, cb])
sql{String | Object} Sql string or query options.values{Any} Values to be format.cb{Function} Callback, arguments:error{Error | undefined} Error if one occurred during the query.results{Array | Object} The results of the query.fields{Array | undefined} Information about the returned results fields (if any).
- Returns: {Query} Query object.
The method calls pool.getConnection to perform a query. Detail to see: connection.query.
pool.escape(value)
value{Any} The value to escape.- Returns: {String} Escaped string value.
Escape a value for SQL. Detali to see, Escaping Query Values.
Default escape options to see mysql.escape, the stringifyObjects and timeZone is set by pool.config.connectionConfig.
pool.escapeId(value)
value{Any} The value to escape.- Returns: {String} Escaped string value.
Escape an identifier for SQL. Detali to see Escaping Query Identifiers.
Default escape options to see mysql.escapeId.
Pool Event
acquire
connection{PoolConnection}
The pool will emit an acquire event when a connection is acquired from the pool. This is called after all acquiring activity has been performed on the connection, right before the connection is handed to the callback of the acquiring code.
pool.on('acquire', function(connection) {
console.log('Connection %d acquired', connection.threadId);
});
connection
connection{PoolConnection}
The pool will emit a connection event when a new connection is made within the pool. If you need to set session variables on the connection before it gets used, you can listen to the connection event.
pool.on('connection', function(connection) {
connection.query('SET SESSION auto_increment_increment=1')
});
enqueue
The pool will emit an enqueue event when a callback has been queued to wait for an available connection.
pool.on('enqueue', function() {
console.log('Waiting for available connection slot');
});
release
connection{PoolConnection}
The pool will emit a release event when a connection is released back to the pool. This is called after all release activity has been performed on the connection, so the connection will be listed as free at the time of the event.
pool.on('release', function(connection) {
console.log('Connection %d released', connection.threadId);
});
PoolCluster Object
The poolCluster object create by mysql.createPoolCluster. PoolCluster provides multiple hosts connection.
poolCluster.add([id, ]config)
id{String} Configuration name. default: automatic id such asCLUSTER::1config{Object} A pool config object.
Add configurations.
Example
// add configurations (the config is a pool config object)
poolCluster.add(config); // add configuration with automatic name
poolCluster.add('MASTER', masterConfig); // add a named configuration
poolCluster.add('SLAVE1', slave1Config);
poolCluster.add('SLAVE2', slave2Config);
poolCluster.remove(pattern)
pattern{String | RegExp} Configurations id orregExpobject which used to match ids. The*in stringpatterncan match any characters.
Remove configurations which match pattern.
Example
// remove configurations
poolCluster.remove('SLAVE2'); // By nodeId
poolCluster.remove('SLAVE*'); // By target group : SLAVE1-2
poolCluster.of([pattern[, selector]])
pattern{String | RegExp} Configurations id orregExpobject which used to match ids. The*in stringpatterncan match any characters.default: '*'selector{String} The selector name to use, default: 'RR'. Detail to see: PoolCluster Options.- Returns: {PoolNamespace}
Get the poolNamespace of configurations. The poolNamespace index by key: pattern+selector.
Example
// of namespace : of(pattern, selector)
poolCluster.of('*').getConnection(function (err, connection) {});
poolCluster.getConnection([pattern[, selector], ]callback)
pattern{String | RegExp} Configurations id orregExpobject which used to match ids. The*in stringpatterncan match any characters.default: '*'selector{String} The selector name to use, default: 'RR'. Detail to see: PoolCluster Options.callback{Function} Arguments:error{Error | undefined} Error object.connection[PoolConnection] Connection object.
Get connection object.
Example
// A pattern can be passed with * as wildcard
poolCluster.getConnection('SLAVE*', 'ORDER', function(err, connection) {});
// The pattern can also be a regular expression
poolCluster.getConnection(/^SLAVE[12]$/, function(err, connection) {});
poolCluster.end([callback])
callback{Function} Arguments:error{Error | undefined} Error object.
Close all connections.
Example
// close all connections
poolCluster.end(function (err) {
// all connections in the pool cluster have ended
});
PoolNamespace Object
poolNamespace.getConnection(callback)
callback{Function} Arguments:error{Error | undefined} Error object.connection[PoolConnection] Connection object.
Get connection object.
Example
var poolNspace = poolCluster.of('SLAVE*', 'RANDOM');
poolNspace.getConnection(function(err, connection) {});
poolNspace.getConnection(function(err, connection) {});
poolNamespace.query(sql[, values][, cb])
sql{String | Object} Sql string or query options.values{Any} Values to be format.cb{Function} Callback, arguments:error{Error | undefined} Error if one occurred during the query.results{Array | Object} The results of the query.fields{Array | undefined} Information about the returned results fields (if any).
- Returns: {Query} Query object.
The method get poolConnection and perform a query. It will release poolConnection when query end.
The method calls connection.query to perform a query. Detail to see: connection.query.
Example
poolNspace.query(sql, function(error, results, fields) {});
Server Disconnects
You may lose the connection to a MySQL server due to network problems, the server timing you out, the server being restarted, or crashing. All of these events are considered fatal errors, and will have the err.code = 'PROTOCOL_CONNECTION_LOST'. See the Error Handling section for more information.
Re-connecting a connection is done by establishing a new connection. Once terminated, an existing connection object cannot be re-connected by design.
With Pool, disconnected connections will be removed from the pool freeing up space for a new connection to be created on the next getConnection call.
With PoolCluster, disconnected connections will count as errors against the related node, incrementing the error code for that node. Once there are more than removeNodeErrorCount errors on a given node, it is removed from the cluster. When this occurs, the PoolCluster may emit a POOL_NONEONLINE error if there are no longer any matching nodes for the pattern. The restoreNodeTimeout config can be set to restore offline nodes after a given timeout.
Escaping Query Values
In order to avoid SQL Injection attacks, you should always escape any user provided data before using it inside a SQL query. You can do so using the mysql.escape(), connection.escape() or pool.escape() methods:
var userId = 'some user provided value';
var sql = 'SELECT * FROM users WHERE id = ' + connection.escape(userId);
connection.query(sql, function(error, results, fields) {
if (error) throw error;
// ...
});
Alternatively, you can use ? characters as placeholders for values you would like to have escaped like this:
connection.query('SELECT * FROM users WHERE id = ?', [userId], function(error, results, fields) {
if (error) throw error;
// ...
});
Multiple placeholders are mapped to values in the same order as passed. For example, in the following query foo equals a, bar equals b, baz equals c, and id will be userId:
connection.query('UPDATE users SET foo = ?, bar = ?, baz = ? WHERE id = ?', ['a', 'b', 'c', userId], function(error, results, fields) {
if (error) throw error;
// ...
});
This looks similar to prepared statements in MySQL, however it really just uses the same connection.escape() method internally.
Different value types are escaped differently, here is how:
- Numbers are left untouched
- Booleans are converted to
true/false - Date objects are converted to
'YYYY-mm-dd HH:ii:ss'strings - Buffers are converted to hex strings, e.g.
X'0fa5' - Strings are safely escaped
- Arrays are turned into list, e.g.
['a', 'b']turns into'a', 'b' - Nested arrays are turned into grouped lists (for bulk inserts), e.g.
[['a', 'b'], ['c', 'd']]turns into('a', 'b'), ('c', 'd') - Objects that have a
toSqlStringmethod will have.toSqlString()called and the returned value is used as the rawSQL. - Objects are turned into
key = 'val'pairs for each enumerable property on the object. If the property's value is a function, it is skipped; if the property's value is an object, toString() is called on it and the returned value is used. undefined/nullare converted toNULLNaN/Infinityare left as-is.MySQLdoes not support these, and trying to insert them as values will triggerMySQLerrors until they implement support.
This escaping allows you to do neat things like this:
var post = {id: 1, title: 'Hello MySQL'};
var query = connection.query('INSERT INTO posts SET ?', post, function(error, results, fields) {
if (error) throw error;
// Neat!
});
console.log(query.sql); // INSERT INTO posts SET `id` = 1, `title` = 'Hello MySQL'
And the toSqlString method allows you to form complex queries with functions:
var CURRENT_TIMESTAMP = { toSqlString: function() { return 'CURRENT_TIMESTAMP()'; } };
var sql = mysql.format('UPDATE posts SET modified = ? WHERE id = ?', [CURRENT_TIMESTAMP, 42]);
console.log(sql); // UPDATE posts SET modified = CURRENT_TIMESTAMP() WHERE id = 42
To generate objects with a toSqlString method, the mysql.raw() method can be used. This creates an object that will be left un-touched when using in a ? placeholder, useful for using functions as dynamic values:
var CURRENT_TIMESTAMP = mysql.raw('CURRENT_TIMESTAMP()');
var sql = mysql.format('UPDATE posts SET modified = ? WHERE id = ?', [CURRENT_TIMESTAMP, 42]);
console.log(sql); // UPDATE posts SET modified = CURRENT_TIMESTAMP() WHERE id = 42
If you feel the need to escape queries by yourself, you can also use the escaping function directly:
var query = "SELECT * FROM posts WHERE title=" + mysql.escape("Hello MySQL");
console.log(query); // SELECT * FROM posts WHERE title='Hello MySQL'
Escaping Query Identifiers
If you can't trust an SQL identifier (database / table / column name) because it is provided by a user, you should escape it with mysql.escapeId(identifier), connection.escapeId(identifier) or pool.escapeId(identifier) like this:
var sorter = 'date';
var sql = 'SELECT * FROM posts ORDER BY ' + connection.escapeId(sorter);
connection.query(sql, function(error, results, fields) {
if (error) throw error;
// ...
});
It also supports adding qualified identifiers. It will escape both parts.
var sorter = 'date';
var sql = 'SELECT * FROM posts ORDER BY ' + connection.escapeId('posts.' + sorter);
// -> SELECT * FROM posts ORDER BY `posts`.`date`
If you do not want to treat . as qualified identifiers, you can set the second argument to true in order to keep the string as a literal identifier:
var sorter = 'date.2';
var sql = 'SELECT * FROM posts ORDER BY ' + connection.escapeId(sorter, true);
// -> SELECT * FROM posts ORDER BY `date.2`
Alternatively, you can use ?? characters as placeholders for identifiers you would like to have escaped like this:
var userId = 1;
var columns = ['username', 'email'];
var query = connection.query('SELECT ?? FROM ?? WHERE id = ?', [columns, 'users', userId], function(error, results, fields) {
if (error) throw error;
// ...
});
console.log(query.sql); // SELECT `username`, `email` FROM `users` WHERE id = 1
When you pass an Object to .escape() or .query(), .escapeId() is used to avoid SQL injection in object keys.
Preparing Queries
You can use mysql.format to prepare a query with multiple insertion points, utilizing the proper escaping for ids and values. A simple example of this follows:
var sql = "SELECT * FROM ?? WHERE ?? = ?";
var inserts = ['users', 'id', userId];
sql = mysql.format(sql, inserts);
Following this you then have a valid, escaped query that you can then send to the database safely. This is useful if you are looking to prepare the query before actually sending it to the database. As mysql.format is exposed from SqlString.format you also have the option (but are not required) to pass in stringifyObject and timezone, allowing you provide a custom means of turning objects into strings, as well as a location-specific/timezone-aware Date.
If you prefer to have another type of query escape format, there's a connection configuration option you can use to define a custom format function. You can access the connection object if you want to use the built-in .escape() or any other connection function.
Here's an example of how to implement another format:
connection.config.queryFormat = function(query, values) {
if (!values) return query;
return query.replace(/\:(\w+)/g, function(txt, key) {
if (values.hasOwnProperty(key)) {
return this.escape(values[key]);
}
return txt;
}.bind(this));
};
connection.query("UPDATE posts SET title = :title", { title: "Hello MySQL" });
Stored Procedures
You can call stored procedures from your queries as with any other mysql driver. If the stored procedure produces several result sets, they are exposed to you the same way as the results for multiple statement queries.
Transactions
Simple transaction support is available at the connection level:
connection.beginTransaction(function(err) {
if (err) { throw err; }
connection.query('INSERT INTO posts SET title=?', title, function(error, results, fields) {
if (error) {
return connection.rollback(function() {
throw error;
});
}
var log = 'Post ' + results.insertId + ' added';
connection.query('INSERT INTO log SET data=?', log, function(error, results, fields) {
if (error) {
return connection.rollback(function() {
throw error;
});
}
connection.commit(function(err) {
if (err) {
return connection.rollback(function() {
throw err;
});
}
console.log('success!');
});
});
});
});
Please note that beginTransaction(), commit() and rollback() are simply convenience functions that execute the START TRANSACTION, COMMIT, and ROLLBACK commands respectively. It is important to understand that many commands in MySQL can cause an implicit commit, as described in the MySQL documentation
Timeouts
Every operation takes an optional inactivity timeout option. This allows you to specify appropriate timeouts for operations. It is important to note that these timeouts are not part of the MySQL protocol, and rather timeout operations through the client. This means that when a timeout is reached, the connection it occurred on will be destroyed and no further operations can be performed.
// Kill query after 60s
connection.query({sql: 'SELECT COUNT(*) AS count FROM big_table', timeout: 60000}, function(error, results, fields) {
if (error && error.code === 'PROTOCOL_SEQUENCE_TIMEOUT') {
throw new Error('too long to count table rows!');
}
if (error) {
throw error;
}
console.log(results[0].count + ' rows');
});
Error Handling
This module comes with a consistent approach to error handling that you should review carefully in order to write solid applications.
Most errors created by this module are instances of the Errorobject. Additionally they typically come with two extra properties:
err.code{String} contains theMySQLserver error symbol if the error is aMySQLserver error (e.g.'ER_ACCESS_DENIED_ERROR').err.errno{Number} contains theMySQLserver error number. Only populated fromMySQLserver error.err.fatal{Boolean} indicating if this error is terminal to the connection object. If the error is not from aMySQLprotocol operation, this property will not be defined.err.sql{String} contains the fullSQLof the failed query. This can be useful when using a higher level interface like anORMthat is generating the queries.err.sqlState{String} contains the five-characterSQLSTATEvalue. Only populated fromMySQLserver error.err.sqlMessage{String} contains the message string that provides a textual description of the error. Only populated fromMySQLserver error.
Fatal errors are propagated to all pending callbacks. In the example below, a fatal error is triggered by trying to connect to an invalid port. Therefore the error object is propagated to both pending callbacks:
var connection = require('mysql').createConnection({
port: 84943, // WRONG PORT
});
connection.connect(function(err) {
console.log(err.code); // 'PROTOCOL_CONNECTION_LOST'
console.log(err.fatal); // true
});
connection.query('SELECT 1', function(error, results, fields) {
console.log(error.code); // 'PROTOCOL_CONNECTION_LOST'
console.log(error.fatal); // true
});
Normal errors however are only delegated to the callback they belong to. So in the example below, only the first callback receives an error, the second query works as expected:
connection.query('USE name_of_db_that_does_not_exist', function(error, results, fields) {
console.log(error.code); // 'ER_BAD_DB_ERROR'
});
connection.query('SELECT 1', function(error, results, fields) {
console.log(error); // null
console.log(results.length); // 1
});
Last but not least: If a fatal errors occurs and there are no pending callbacks, or a normal error occurs which has no callback belonging to it, the error is emitted as an 'error' event on the connection object. This is demonstrated in the example below:
connection.on('error', function(err) {
console.log(err.code); // 'ER_BAD_DB_ERROR'
});
connection.query('USE name_of_db_that_does_not_exist');
This module does not want you to deal with silent failures. You should always provide callbacks to your method calls. If you want to ignore this advice and suppress unhandled errors, you can do this:
// I am Chuck Norris:
connection.on('error', function() {});
Exception Safety
This module is exception safe. That means you can continue to use it, even if one of your callback functions throws an error which you're catching using 'uncaughtException'.
Debugging And Reporting Problems
If you are running into problems, one thing that may help is enabling the debug mode for the connection:
var connection = mysql.createConnection({debug: true});
This will print all incoming and outgoing packets on stdout. You can also restrict debugging to packet types by passing an array of types to debug to restrict debugging to the query and data packets:
var connection = mysql.createConnection({debug: ['ComQueryPacket', 'RowDataPacket']});
Example
- Base connection exam.
var mysql = require('mysql');
var connection = mysql.createConnection({
host: '192.168.128.102',
user: 'admin',
password: '12345678',
database: 'mydb',
});
connection.connect();
// st(id, name, age)
connection.query('SELECT * FROM st;', function(error, results, fields) {
if (error) throw error;
console.log('Results[0]: ', results[0]); // Results[0]: {id:1,name:'name1',age:18}
});
connection.on('error', function (err) {
console.log(err.message);
});
connection.end();
require('iosched').forever();
- Base pool connections exam.
var mysql = require('mysql');
var pool = mysql.createPool({
host: '192.168.128.102',
user: 'admin',
password: '12345678',
database: 'mydb',
});
pool.getConnection(function(err, connection) {
if (err) throw err; // Not connected!
// Use the connection
connection.query('SELECT * FROM st;', function(error, results, fields) {
// When done with the connection, release it.
connection.release();
// Handle error after the release.
if (error) throw error;
console.log('Results[0]: ', results[0]); // Results[0]: {id:1,name:'name1',age:18}
// Don't use the connection here, it has been returned to the pool.
});
});
require('iosched').forever();
- Base pool cluster exam.
var mysql = require('mysql');
var poolCluster = mysql.createPoolCluster();
poolCluster.add('MASTER', {
host: '192.168.128.102',
user: 'admin',
password: '12345678',
database: 'mydb',
}); // Add a named configuration
// Target Group : MASTER, Selector : round-robin
poolCluster.getConnection('MASTER', function(err, connection) {
if (err) throw err; // Not connected!
// Use the connection
connection.query('SELECT * FROM st;', function(error, results, fields) {
// When done with the connection, release it.
connection.release();
// Handle error after the release.
if (error) throw error;
console.log('Results[0]: ', results[0]); // Results[0]: {id:1,name:'name1',age:18}
// Don't use the connection here, it has been returned to the pool.
});
});
require('iosched').forever();




陕公网安备61019002002605号